/*
 * @Description: 数组扁平化
 * @Author: wang_k
 * @Date: 2022-12-09 14:32:07
 * @LastEditTime: 2022-12-09 15:02:55
 * @FilePath: /测试文档(总结)/51.数组扁平化.js
 */
const arr = [
  {
    label: '水果',
    children: [
      {
        label: '香蕉'
      },
      {
        label: '橘子'
      }
    ]
  },
  {
    label: '蔬菜',
    children: [
      {
        label: '西红柿'
      },
      {
        label: '白菜'
      }
    ]
  },
  {
    label: '小猫'
  },
  {
    label: '小狗'
  }
]
const func = (res) => res.reduce((prev, cur) => {
  // console.log(prev, cur)
  return prev.concat(Array.isArray(cur.children) ? func(cur.children) : cur)
}, [])
console.log(func(arr))

// 利用 reduce() 求数组的平均值 
const average = (arr) => arr.reduce((a, b) => a + b) / arr.length;
average([1,9,18,36]) //16